package com.example.zproject.controller;

import cn.hutool.core.net.URLEncodeUtil;
import com.example.zproject.entity.UserEntity;
import com.example.zproject.service.impl.UserServerimpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/user/register")
public class UserRegisterController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("注册用户");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String confirmPwd = req.getParameter("confirmPassword");
        System.out.println("用户注册数据"+username+password+email+confirmPwd);

        //校验要求：用户名：6位以上的数字字母组合
        //密码：8位以上，包含数字字母
        //邮箱：合法的邮箱格式
        //确认密码：与密码相同
        String usernameRegex = "^[a-zA-Z0-9]{6,}$";
        String passwordRegex = "^[a-zA-Z0-9]{6,}$";
        String emailRegex = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$";



        //  一般都推荐判断非法情况；快速结束;
        if (!confirmPwd.equals(password)){ // 密码 = 确认密码
            gotoRegisterPage(req, resp,"输入数据不正确，请核对");
            return;
        }

        if(!username.matches(usernameRegex)){
            //出现非法情况
            gotoRegisterPage(req, resp,"输入数据不正确，请核对");
            return;
        }
        if(!password.matches(passwordRegex)){
            //出现非法情况
            gotoRegisterPage(req, resp,"输入数据不正确，请核对");
            return;
        }

        // match： 返回true
        if (!email.matches(emailRegex)){
            //出现非法情况
            gotoRegisterPage(req, resp,"输入数据不正确，请核对");
            //
            return;
        }
        System.out.println("数据校验通过，准备注册用户");

        UserServerimpl userServer = new UserServerimpl();

        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPassword(password);
        userEntity.setEmail(email);

        try {
            userServer.register(userEntity);
            String encode = URLEncodeUtil.encode("注册成功，请登录");
            resp.sendRedirect("/z_project_war_exploded/login.jsp?encode="+encode);
//            req.setAttribute("msg", "注册成功，请登录");
//            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }catch (SQLException e) {
            System.out.println(e.getClass());
            e.printStackTrace();
            gotoRegisterPage(req, resp,"用户邮箱已存在");
        }
        catch (Exception e) {
            System.out.println(e.getClass());
            e.printStackTrace();
            gotoRegisterPage(req, resp,"当前注册用户太多请稍后再试");
        }
    }

    private void gotoRegisterPage(HttpServletRequest req, HttpServletResponse resp, String s) throws ServletException, IOException {
        req.setAttribute("msg", s );
        req.getRequestDispatcher("/register.jsp").forward(req, resp);
    }
}
